package com.kistring.polygyny;

import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import com.kistring.database.AccelerometerDB;

public class Kitsai extends Service implements SensorEventListener {
	private Sensor mAccelerometer;
	private SensorManager mSensorManager;
	private List<Sensor> mSensors;
	private float gravity[] = new float[3];
	private float linear_acceleration[] = new float[3];
	private PowerManager mPowerManager;
	private boolean token = false;
	private AccelerometerDB accelerometerDB;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// Get an instance of the SensorManager
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		mSensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
		if (mSensors.size() > 0) {
			mSensorManager.registerListener(this, mSensors.get(0),
					SensorManager.SENSOR_DELAY_FASTEST);
		} else {
		}
		// Get an instance of the PowerManager
		mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
		this.accelerometerDB = new AccelerometerDB(this);
		
		new Thread(giveToken).start();
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		mSensorManager.unregisterListener(this);
		super.onDestroy();
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// alpha is calculated as t / (t + dT)
		// with t, the low-pass filter's time-constant
		// and dT, the event delivery rate

		final float alpha = (float) 0.8;

		gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
		gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
		gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

		linear_acceleration[0] = event.values[0] - gravity[0];
		linear_acceleration[1] = event.values[1] - gravity[1];
		linear_acceleration[2] = event.values[2] - gravity[2];

		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			if (token) {
				this.accelerometerDB.insert(linear_acceleration[0], linear_acceleration[1], linear_acceleration[2], event.timestamp);
				
				Log.i("ACCELEROMETER", String.valueOf(linear_acceleration[0])
						+ ", " + String.valueOf(linear_acceleration[1]) + ", "
						+ String.valueOf(linear_acceleration[2]) + "; "
						+ String.valueOf(event.timestamp));
				token = false;
			}
		}

	}

	private Runnable giveToken = new Runnable() {
		public void run() {
			while (true) {
				try {
					Thread.sleep(1000);
					token = true;
				} catch (Exception ee) {
					Log.i("ACCELEROMETER", ee.getMessage(), ee);
				}
			}
		}
	};
}
